/*     */ package com.zimbra.qa.unittest.prov.ldap;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AccountServiceException;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.ldap.unboundid.InMemoryLdapServer;
/*     */ import com.zimbra.qa.unittest.TestUtil;
/*     */ import com.zimbra.qa.unittest.prov.BinaryLdapData.Content;
/*     */ import com.zimbra.soap.admin.type.CacheEntryType;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import org.junit.AfterClass;
/*     */ import org.junit.Assert;
/*     */ import org.junit.BeforeClass;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestLdapProvModifyAttrs
/*     */   extends LdapTest
/*     */ {
/*     */   private static LdapProvTestUtil provUtil;
/*     */   private static Provisioning prov;
/*     */   private static Domain domain;
/*     */   
/*     */   @BeforeClass
/*     */   public static void init()
/*     */     throws Exception
/*     */   {
/*  44 */     provUtil = new LdapProvTestUtil();
/*  45 */     prov = provUtil.getProv();
/*  46 */     domain = provUtil.createDomain(baseDomainName());
/*     */   }
/*     */   
/*     */   @AfterClass
/*     */   public static void cleanup() throws Exception {
/*  51 */     Cleanup.deleteAll(new String[] { baseDomainName() });
/*     */   }
/*     */   
/*     */   private Account createAccount(String localPart) throws Exception {
/*  55 */     return createAccount(localPart, null);
/*     */   }
/*     */   
/*     */   private Account createAccount(String localPart, Map<String, Object> attrs) throws Exception {
/*  59 */     String acctName = TestUtil.getAddress(localPart, domain.getName());
/*  60 */     Account acct = prov.get(Key.AccountBy.name, acctName);
/*  61 */     Assert.assertNull(acct);
/*     */     
/*  63 */     acct = prov.createAccount(acctName, "test123", attrs);
/*  64 */     Assert.assertNotNull(acct);
/*  65 */     return acct;
/*     */   }
/*     */   
/*     */   private void deleteAccount(Account acct) throws Exception {
/*  69 */     String acctId = acct.getId();
/*  70 */     prov.deleteAccount(acctId);
/*  71 */     acct = prov.get(Key.AccountBy.id, acctId);
/*  72 */     Assert.assertNull(acct);
/*     */   }
/*     */   
/*     */   private Account getFresh(Account acct) throws Exception
/*     */   {
/*  77 */     prov.flushCache(CacheEntryType.account, null);
/*  78 */     return prov.get(Key.AccountBy.id, acct.getId());
/*     */   }
/*     */   
/*     */   private void modifySingleValue(Account acct, String attrName, String attrValue) throws Exception {
/*  82 */     Map<String, Object> attrs = new HashMap();
/*  83 */     attrs.put(attrName, attrValue);
/*  84 */     prov.modifyAttrs(acct, attrs);
/*  85 */     acct = getFresh(acct);
/*     */     
/*     */ 
/*  88 */     String expectedValue = attrValue == "" ? null : attrValue;
/*  89 */     Assert.assertEquals(expectedValue, acct.getAttr(attrName));
/*     */   }
/*     */   
/*     */   private void modifyMultiValue(Account acct, String attrName, String[] attrValues) throws Exception {
/*  93 */     Map<String, Object> attrs = new HashMap();
/*  94 */     attrs.put(attrName, attrValues);
/*  95 */     prov.modifyAttrs(acct, attrs);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void setSingleValue() throws Exception {
/* 100 */     String ATTR_NAME = "displayName";
/* 101 */     String ATTR_VALUE = "setSingleValue";
/*     */     
/* 103 */     Account acct = createAccount(genAcctNameLocalPart());
/*     */     
/*     */ 
/* 106 */     modifySingleValue(acct, ATTR_NAME, ATTR_VALUE);
/* 107 */     modifySingleValue(acct, ATTR_NAME, ATTR_VALUE);
/*     */     
/* 109 */     deleteAccount(acct);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void setSingleValueBinary() throws Exception {
/* 114 */     String ATTR_NAME = "userSMIMECertificate";
/* 115 */     String ATTR_VALUE = BinaryLdapData.Content.generateContent(32).getString();
/*     */     
/* 117 */     Account acct = createAccount(genAcctNameLocalPart());
/*     */     
/*     */ 
/* 120 */     modifySingleValue(acct, ATTR_NAME, ATTR_VALUE);
/* 121 */     modifySingleValue(acct, ATTR_NAME, ATTR_VALUE);
/*     */     
/* 123 */     deleteAccount(acct);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void unsetSingleValue() throws Exception {
/* 128 */     String ATTR_NAME = "displayName";
/* 129 */     String ATTR_VALUE = "unsetSingleValue";
/*     */     
/* 131 */     Account acct = createAccount(genAcctNameLocalPart());
/* 132 */     modifySingleValue(acct, ATTR_NAME, ATTR_VALUE);
/*     */     
/*     */ 
/* 135 */     modifySingleValue(acct, ATTR_NAME, null);
/* 136 */     modifySingleValue(acct, ATTR_NAME, null);
/* 137 */     modifySingleValue(acct, ATTR_NAME, "");
/* 138 */     modifySingleValue(acct, ATTR_NAME, "");
/*     */     
/* 140 */     deleteAccount(acct);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void addMultiValue() throws Exception {
/* 145 */     String ATTR_NAME = "zimbraACE";
/* 146 */     String ATTR_VALUE_1 = "addMultiValue-1";
/* 147 */     String ATTR_VALUE_2 = "addMultiValue-2";
/* 148 */     String ATTR_VALUE_3 = "addMultiValue-3";
/* 149 */     String ATTR_VALUE_4 = "addMultiValue-4";
/*     */     
/* 151 */     Account acct = createAccount(genAcctNameLocalPart());
/* 152 */     modifyMultiValue(acct, "+" + ATTR_NAME, new String[] { ATTR_VALUE_1, ATTR_VALUE_2 });
/* 153 */     modifyMultiValue(acct, "+" + ATTR_NAME, new String[] { ATTR_VALUE_3, ATTR_VALUE_4 });
/* 154 */     modifyMultiValue(acct, "+" + ATTR_NAME, new String[] { ATTR_VALUE_1, ATTR_VALUE_2 });
/* 155 */     modifyMultiValue(acct, "+" + ATTR_NAME, new String[] { ATTR_VALUE_3, ATTR_VALUE_4 });
/*     */     
/* 157 */     acct = getFresh(acct);
/* 158 */     Set<String> values = acct.getMultiAttrSet(ATTR_NAME);
/* 159 */     Assert.assertEquals(4L, values.size());
/* 160 */     Assert.assertTrue(values.contains(ATTR_VALUE_1));
/* 161 */     Assert.assertTrue(values.contains(ATTR_VALUE_2));
/* 162 */     Assert.assertTrue(values.contains(ATTR_VALUE_3));
/* 163 */     Assert.assertTrue(values.contains(ATTR_VALUE_4));
/*     */     
/* 165 */     deleteAccount(acct);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void addMultiValueBinary() throws Exception {
/* 170 */     String ATTR_NAME = "userSMIMECertificate";
/* 171 */     String ATTR_VALUE_1 = BinaryLdapData.Content.generateContent(32).getString();
/* 172 */     String ATTR_VALUE_2 = BinaryLdapData.Content.generateContent(32).getString();
/* 173 */     String ATTR_VALUE_3 = BinaryLdapData.Content.generateContent(32).getString();
/* 174 */     String ATTR_VALUE_4 = BinaryLdapData.Content.generateContent(32).getString();
/*     */     
/* 176 */     Account acct = createAccount(genAcctNameLocalPart());
/* 177 */     modifyMultiValue(acct, "+" + ATTR_NAME, new String[] { ATTR_VALUE_1, ATTR_VALUE_2 });
/*     */     
/* 179 */     acct = getFresh(acct);
/* 180 */     Set<String> values = acct.getMultiAttrSet(ATTR_NAME);
/* 181 */     Assert.assertEquals(2L, values.size());
/* 182 */     Assert.assertTrue(values.contains(ATTR_VALUE_1));
/* 183 */     Assert.assertTrue(values.contains(ATTR_VALUE_2));
/*     */     
/* 185 */     deleteAccount(acct);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void removeMultiValue() throws Exception {
/* 190 */     String ATTR_NAME = "zimbraACE";
/* 191 */     String ATTR_VALUE_1 = "removeMultiValue-1";
/* 192 */     String ATTR_VALUE_2 = "removeMultiValue-2";
/* 193 */     String ATTR_VALUE_3 = "removeMultiValue-3";
/* 194 */     String ATTR_VALUE_4 = "removeMultiValue-4";
/*     */     
/* 196 */     Account acct = createAccount(genAcctNameLocalPart());
/* 197 */     modifyMultiValue(acct, "+" + ATTR_NAME, new String[] { ATTR_VALUE_1, ATTR_VALUE_2, ATTR_VALUE_3, ATTR_VALUE_4 });
/*     */     
/* 199 */     modifyMultiValue(acct, "-" + ATTR_NAME, new String[] { ATTR_VALUE_3, ATTR_VALUE_4 });
/*     */     
/* 201 */     acct = getFresh(acct);
/* 202 */     Set<String> values = acct.getMultiAttrSet(ATTR_NAME);
/* 203 */     Assert.assertEquals(2L, values.size());
/* 204 */     Assert.assertTrue(values.contains(ATTR_VALUE_1));
/* 205 */     Assert.assertTrue(values.contains(ATTR_VALUE_2));
/*     */     
/* 207 */     deleteAccount(acct);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void unsetMultiValue() throws Exception {
/* 212 */     String ATTR_NAME = "zimbraACE";
/* 213 */     String ATTR_VALUE_1 = "removeMultiValue-1";
/* 214 */     String ATTR_VALUE_2 = "removeMultiValue-2";
/* 215 */     String ATTR_VALUE_3 = "removeMultiValue-3";
/* 216 */     String ATTR_VALUE_4 = "removeMultiValue-4";
/*     */     
/* 218 */     Account acct = createAccount(genAcctNameLocalPart());
/* 219 */     modifyMultiValue(acct, "+" + ATTR_NAME, new String[] { ATTR_VALUE_1, ATTR_VALUE_2, ATTR_VALUE_3, ATTR_VALUE_4 });
/*     */     
/* 221 */     modifyMultiValue(acct, ATTR_NAME, null);
/*     */     
/* 223 */     acct = getFresh(acct);
/* 224 */     Set<String> values = acct.getMultiAttrSet(ATTR_NAME);
/* 225 */     Assert.assertEquals(0L, values.size());
/*     */     
/* 227 */     deleteAccount(acct);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void invalidAttrValue() throws Exception
/*     */   {
/* 233 */     String ATTR_NAME = "zimbraPrefSkin";
/* 234 */     String ATTR_VALUE_1 = "invalidAttrValue-1";
/* 235 */     String ATTR_VALUE_2 = "invalidAttrValue-2";
/*     */     
/* 237 */     Account acct = createAccount(genAcctNameLocalPart());
/*     */     
/* 239 */     boolean caughtException = false;
/*     */     try {
/* 241 */       modifyMultiValue(acct, "+" + ATTR_NAME, new String[] { ATTR_VALUE_1, ATTR_VALUE_2 });
/*     */     } catch (AccountServiceException e) {
/* 243 */       if ("account.INVALID_ATTR_VALUE".equals(e.getCode())) {
/* 244 */         caughtException = true;
/*     */       }
/*     */     } catch (ServiceException e) {
/* 247 */       if (InMemoryLdapServer.isOn())
/*     */       {
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 253 */         if ("service.FAILURE".equals(e.getCode())) {
/* 254 */           caughtException = true;
/*     */         }
/*     */       }
/*     */     }
/* 258 */     Assert.assertTrue(caughtException);
/*     */     
/* 260 */     deleteAccount(acct);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void invalidAttrName() throws Exception
/*     */   {
/* 266 */     String ATTR_NAME = "bogus";
/* 267 */     String ATTR_VALUE = "invalidAttrValue";
/*     */     
/* 269 */     Account acct = createAccount(genAcctNameLocalPart());
/*     */     
/* 271 */     boolean caughtException = false;
/*     */     try {
/* 273 */       modifySingleValue(acct, "+" + ATTR_NAME, ATTR_VALUE);
/*     */     } catch (AccountServiceException e) {
/* 275 */       if ("account.INVALID_ATTR_NAME".equals(e.getCode())) {
/* 276 */         caughtException = true;
/*     */       }
/*     */     } catch (ServiceException e) {
/* 279 */       if (InMemoryLdapServer.isOn())
/*     */       {
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 285 */         if ("service.FAILURE".equals(e.getCode())) {
/* 286 */           caughtException = true;
/*     */         }
/*     */       }
/*     */     }
/* 290 */     Assert.assertTrue(caughtException);
/*     */     
/* 292 */     deleteAccount(acct);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/prov/ldap/TestLdapProvModifyAttrs.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */